package com.atguigu.gulimall.member.service.impl;

import com.atguigu.gulimall.member.controller.vo.MemberVo;
import com.atguigu.gulimall.member.exception.PhoneExsitException;
import com.atguigu.gulimall.member.exception.UserNameExsitException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.member.dao.MemberDao;
import com.atguigu.gulimall.member.entity.MemberEntity;
import com.atguigu.gulimall.member.service.MemberService;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void register(MemberVo memberVo) {
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setLevelId(1L);
        //保存之前检查用户名的唯一性  为了能让controller感知异常，使用异常机制
        this.checkPhoneUnique(memberVo.getPhone());
        this.checkUserNameUnique(memberVo.getUserName());
        memberEntity.setUsername(memberVo.getUserName());
        //密码不能使用明文，密码需要加密存储 通过Spring自带的加密工具进行加密，同一个字符串每一个加密出来的值都不一样，但是有match方法进行匹配
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode(memberVo.getPassword());
//        passwordEncoder.matches(memberVo.getPassword(),encode);
        memberEntity.setPassword(encode);
        memberEntity.setMobile(memberVo.getPhone());
        this.baseMapper.insert(memberEntity);
    }

    @Override
    public void checkUserNameUnique(String userName) throws UserNameExsitException {
        QueryWrapper<MemberEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", userName);
        Integer integer = this.baseMapper.selectCount(queryWrapper);
        if (integer > 0) {
            throw new UserNameExsitException();
        }
    }

    @Override
    public void checkPhoneUnique(String phone) throws PhoneExsitException {
        QueryWrapper<MemberEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile", phone);
        Integer integer = this.baseMapper.selectCount(queryWrapper);
        if (integer > 0) {
            throw new PhoneExsitException();
        }
    }

    @Override
    public MemberEntity login(MemberVo memberVo) {
        QueryWrapper<MemberEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", memberVo.getUserName()).or().eq("mobile", memberVo.getUserName());
        MemberEntity memberEntity = this.baseMapper.selectOne(queryWrapper);
        if (memberEntity != null) {
            BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
            boolean matches = encoder.matches(memberVo.getPassword(), memberEntity.getPassword());
            if (matches) {
                return memberEntity;
            }
            return null;
        }
        return null;
    }

}
